﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SimioAPI;
using SimioAPI.Extensions;
using System.Windows.Forms;
using System.Collections;


namespace WLC
{
    class WLCUpdateOrderStatisticStepDefinition : IStepDefinition
    {
        #region IStepDefinition Members

        public IStep CreateStep(IPropertyReaders properties)
        {
            return new WLCUpdateOrderStatisticStep(properties);
        }

        public void DefineSchema(IPropertyDefinitions schema)
        {
            IPropertyDefinition orderStatElement = schema.AddElementProperty("OrderStatisticsElement", WLCOrderStatisticsElementDefinition.ID);
            orderStatElement.Description = "The order statistics element that manages the statistics for the orders.";
            orderStatElement.Required = true;

            IPropertyDefinition statisticName = schema.AddStringProperty("StatisticName", string.Empty);
            statisticName.Description = "The name of the statistic to update for this order.";
            statisticName.Required = true;

            IPropertyDefinition value = schema.AddExpressionProperty("Value", string.Empty);
            value.Description = "The value to add to the order's statistic.";
            value.Required = true;
        }

        public int NumberOfExits
        {
            get { return 1; }
        }

        public string Name
        {
            get { return "UpdateOrderStatistic"; }
        }

        public string Description
        {
            get { return ""; }
        }

        public System.Drawing.Image Icon
        {
            get { return null; }
        }

        public Guid UniqueID
        {
            get { return new Guid("{B83F6DA0-195F-11DF-A504-F90256D89593}"); }
        }

        #endregion
    }


    class WLCUpdateOrderStatisticStep : IStep
    {

        IPropertyReader _statisticName;
        IPropertyReader _valueProp;
        IElementProperty _orderStatisticElementProp;
                

        public WLCUpdateOrderStatisticStep(IPropertyReaders bag)
        {
            _statisticName = bag.GetProperty("StatisticName");
            _valueProp = bag.GetProperty("Value");
            _orderStatisticElementProp = (IElementProperty)bag.GetProperty("OrderStatisticsElement");
                     
        }

        #region IStep Members

        private string GetOrderType(IStepExecutionContext context)
        {
            string rawType = context.AssociatedObject.ExecutionContext.ToString();
            string[] splitType = rawType.Split(' ');
            
            
            return splitType[1];   
        }

        public ExitType Execute(IStepExecutionContext context)
        {


            string orderType = GetOrderType(context);
            string statisticName = _statisticName.GetStringValue(context);
            double value = _valueProp.GetDoubleValue(context);
            WLCOrderStatisticsElement orderStatisticElement = (WLCOrderStatisticsElement)_orderStatisticElementProp.GetElement(context);

            orderStatisticElement.AddStatisticValue(orderType, statisticName, value);
                       
            return ExitType.FirstExit;
        }

        #endregion

    }

    
}
